home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / ziodev.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  7.0 KB  |  246 lines

  1. /* Copyright (C) 1993, 1995, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: ziodev.c,v 1.3 2000/09/19 19:00:54 lpd Exp $ */
  20. /* Standard IODevice implementation */
  21. #include "memory_.h"
  22. #include "stdio_.h"
  23. #include "string_.h"
  24. #include "ghost.h"
  25. #include "gp.h"
  26. #include "gpcheck.h"
  27. #include "oper.h"
  28. #include "stream.h"
  29. #include "ialloc.h"
  30. #include "iscan.h"
  31. #include "ivmspace.h"
  32. #include "gxiodev.h"        /* must come after stream.h */
  33.                 /* and before files.h */
  34. #include "files.h"
  35. #include "scanchar.h"        /* for char_EOL */
  36. #include "store.h"
  37.  
  38. /* Import the dtype of the stdio IODevices. */
  39. extern const char iodev_dtype_stdio[];
  40.  
  41. /* Define the special devices. */
  42. #define iodev_special(dname, init, open) {\
  43.     dname, iodev_dtype_stdio,\
  44.     { init, open, iodev_no_open_file, iodev_no_fopen, iodev_no_fclose,\
  45.       iodev_no_delete_file, iodev_no_rename_file, iodev_no_file_status,\
  46.       iodev_no_enumerate_files, NULL, NULL,\
  47.       iodev_no_get_params, iodev_no_put_params\
  48.     }\
  49. }
  50.  
  51. /*
  52.  * We need the current context pointer for accessing / opening the %std
  53.  * IODevices.  However, this is not available to the open routine.
  54.  * Therefore, we use the hack of storing this pointer in the IODevice state
  55.  * pointer just before calling the open routines.  We clear the pointer
  56.  * immediately afterwards so as not to wind up with dangling references.
  57.  */
  58.  
  59. #define LINEEDIT_BUF_SIZE 20    /* initial size, not fixed size */
  60. private iodev_proc_open_device(lineedit_open);
  61. const gx_io_device gs_iodev_lineedit =
  62.     iodev_special("%lineedit%", iodev_no_init, lineedit_open);
  63.  
  64. #define STATEMENTEDIT_BUF_SIZE 50    /* initial size, not fixed size */
  65. private iodev_proc_open_device(statementedit_open);
  66. const gx_io_device gs_iodev_statementedit =
  67.     iodev_special("%statementedit%", iodev_no_init, statementedit_open);
  68.  
  69. /* ------ Operators ------ */
  70.  
  71. /* <int> .getiodevice <string|null> */
  72. private int
  73. zgetiodevice(i_ctx_t *i_ctx_p)
  74. {
  75.     os_ptr op = osp;
  76.     gx_io_device *iodev;
  77.     const byte *dname;
  78.  
  79.     check_type(*op, t_integer);
  80.     if (op->value.intval != (int)op->value.intval)
  81.     return_error(e_rangecheck);
  82.     iodev = gs_getiodevice((int)(op->value.intval));
  83.     if (iodev == 0)        /* index out of range */
  84.     return_error(e_rangecheck);
  85.     dname = (const byte *)iodev->dname;
  86.     if (dname == 0)
  87.     make_null(op);
  88.     else
  89.     make_const_string(op, a_readonly | avm_foreign,
  90.               strlen((const char *)dname), dname);
  91.     return 0;
  92. }
  93.  
  94. /* ------ %lineedit and %statementedit ------ */
  95.  
  96. private int
  97. line_collect(gx_io_device * iodev, const char *access, stream ** ps,
  98.          gs_memory_t * mem, uint initial_buf_size, bool statement)
  99. {
  100.     uint count = 0;
  101.     bool in_eol = false;
  102.     int code;
  103.     gx_io_device *indev = gs_findiodevice((const byte *)"%stdin", 6);
  104.     /* HACK: get the context pointer from the IODevice.  See above. */
  105.     i_ctx_t *i_ctx_p = (i_ctx_t *)iodev->state;
  106.     stream *s;
  107.     stream *ins;
  108.     gs_string str;
  109.     /*
  110.      * buf exists only for stylistic parallelism: all occurrences of
  111.      * buf-> could just as well be str. .
  112.      */
  113.     gs_string *const buf = &str;
  114.  
  115.     if (strcmp(access, "r"))
  116.     return_error(e_invalidfileaccess);
  117.     s = file_alloc_stream(mem, "line_collect(stream)");
  118.     if (s == 0)
  119.     return_error(e_VMerror);
  120.     /* HACK: see above. */
  121.     indev->state = i_ctx_p;
  122.     code = (indev->procs.open_device)(indev, access, &ins, mem);
  123.     indev->state = 0;
  124.     if (code < 0)
  125.     return code;
  126.     buf->size = initial_buf_size;
  127.     buf->data = gs_alloc_string(mem, buf->size, "line_collect(buffer)");
  128.     if (buf->data == 0)
  129.     return_error(e_VMerror);
  130. rd:
  131.     /*
  132.      * We have to stop 1 character short of the buffer size,
  133.      * because %statementedit must append an EOL.
  134.      */
  135.     buf->size--;
  136.     /* HACK: set %stdin's state so that GNU readline can retrieve it. */
  137.     indev->state = i_ctx_p;
  138.     code = zreadline_from(ins, buf, mem, &count, &in_eol);
  139.     buf->size++;        /* restore correct size */
  140.     indev->state = 0;
  141.     switch (code) {
  142.     case EOFC:
  143.         code = gs_note_error(e_undefinedfilename);
  144.         /* falls through */
  145.     case 0:
  146.         break;
  147.     default:
  148.         code = gs_note_error(e_ioerror);
  149.         break;
  150.     case 1:        /* filled buffer */
  151.         {
  152.         uint nsize;
  153.         byte *nbuf;
  154.  
  155. #if arch_ints_are_short
  156.         if (nsize == max_uint) {
  157.             code = gs_note_error(e_limitcheck);
  158.             break;
  159.         } else if (nsize >= max_uint / 2)
  160.             nsize = max_uint;
  161.         else
  162. #endif
  163.             nsize = buf->size * 2;
  164.         nbuf = gs_resize_string(mem, buf->data, buf->size, nsize,
  165.                     "line_collect(grow buffer)");
  166.         if (nbuf == 0) {
  167.             code = gs_note_error(e_VMerror);
  168.             break;
  169.         }
  170.         buf->data = nbuf;
  171.         buf->size = nsize;
  172.         goto rd;
  173.         }
  174.     }
  175.     if (code != 0) {
  176.     gs_free_string(mem, buf->data, buf->size, "line_collect(buffer)");
  177.     return code;
  178.     }
  179.     if (statement) {
  180.     /* If we don't have a complete token, keep going. */
  181.     stream st;
  182.     stream *ts = &st;
  183.     scanner_state state;
  184.     ref ignore_value;
  185.     uint depth = ref_stack_count(&o_stack);
  186.     int code;
  187.  
  188.     /* Add a terminating EOL. */
  189.     buf->data[count++] = char_EOL;
  190.     sread_string(ts, buf->data, count);
  191. sc:
  192.     scanner_state_init_check(&state, false, true);
  193.     code = scan_token(i_ctx_p, ts, &ignore_value, &state);
  194.     ref_stack_pop_to(&o_stack, depth);
  195.     switch (code) {
  196.         case 0:        /* read a token */
  197.         case scan_BOS:
  198.         goto sc;    /* keep going until we run out of data */
  199.         case scan_Refill:
  200.         goto rd;
  201.         case scan_EOF:
  202.         break;
  203.         default:        /* error */
  204.         gs_free_string(mem, buf->data, buf->size, "line_collect(buffer)");
  205.         return code;
  206.     }
  207.     }
  208.     buf->data = gs_resize_string(mem, buf->data, buf->size, count,
  209.                "line_collect(resize buffer)");
  210.     if (buf->data == 0)
  211.     return_error(e_VMerror);
  212.     sread_string(s, buf->data, count);
  213.     s->save_close = s->procs.close;
  214.     s->procs.close = file_close_disable;
  215.     *ps = s;
  216.     return 0;
  217. }
  218.  
  219. private int
  220. lineedit_open(gx_io_device * iodev, const char *access, stream ** ps,
  221.           gs_memory_t * mem)
  222. {
  223.     int code = line_collect(iodev, access, ps, mem,
  224.                 LINEEDIT_BUF_SIZE, false);
  225.  
  226.     return (code < 0 ? code : 1);
  227. }
  228.  
  229. private int
  230. statementedit_open(gx_io_device * iodev, const char *access, stream ** ps,
  231.            gs_memory_t * mem)
  232. {
  233.     int code = line_collect(iodev, access, ps, mem,
  234.                 STATEMENTEDIT_BUF_SIZE, true);
  235.  
  236.     return (code < 0 ? code : 1);
  237. }
  238.  
  239. /* ------ Initialization procedure ------ */
  240.  
  241. const op_def ziodev_op_defs[] =
  242. {
  243.     {"1.getiodevice", zgetiodevice},
  244.     op_def_end(0)
  245. };
  246.